package org.putrpctorest.expression;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.helpers.DOMUtils;
import org.putrpctorest.PutRpcToRestError;
import org.w3c.dom.Element;

public class ParameterGroupParser extends ExpressionParser {
	
	private final static String REST_ENTRY_POINT_NS = "http://www.example.org/rest-expressions";

	

	public AbstractExpression parseRouter(String id, Element config) {
		ParameterGroup parameterGroup = new ParameterGroup(id);
		
		verifiyConfig(config);

		Element methodElement = DOMUtils.getFirstChildWithName(
				config, REST_ENTRY_POINT_NS, "parameter");
		
		List<String> parameters = new ArrayList<String>();
		
		while(methodElement != null) {
			
			parameters.add(methodElement.getAttribute("name"));
			
			methodElement = DOMUtils.getNextElement(methodElement);
		}
		
		parameterGroup.setParameterList(parameters);
		

		return parameterGroup;
	}


	private void verifiyConfig(Element config) {

		
		Element javaMethodEl = DOMUtils.getFirstChildWithName(
				config, REST_ENTRY_POINT_NS, "parameter");
		
		if (javaMethodEl == null) {
			throw new PutRpcToRestError(
			"could not find parameter element inside config!");
		}
	}

	public String getType() {
		return "parameter-group";
	}

}
